package com.maiji.cloud.utils;

import com.alibaba.fastjson.JSON;
import com.maiji.cloud.entities.login.AppUser;
import com.maiji.cloud.microservice.UserInfoService;
import com.maiji.cloud.service.AppUserService;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class RedisUtil {

    private static final long EXPIRELONG = 15;
    private static final TimeUnit TIMEUNIT = TimeUnit.MINUTES;

    /**
     * 向缓存中添加时效数据
     * @param redisTemplate
     * @param key
     * @param obj
     * @return
     */
    public static void setObject (RedisTemplate redisTemplate, String key, Object obj) {
        redisTemplate.opsForValue().set(key, obj, EXPIRELONG, TIMEUNIT);
    }

    public static void setObject2 (RedisTemplate redisTemplate, Integer myDataBase, String key, Object obj) {
        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        jedisConnectionFactory.setDatabase(0);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.opsForValue().set(key, obj, EXPIRELONG, TIMEUNIT);
        jedisConnectionFactory.setDatabase(myDataBase);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
    }

    /**
     *  非AppLogin微服务调用
     *  再redis数据库0中取出AppUser
     * @param redisTemplate 当前服务注入的redisTemplate
     * @param myDataBase 当前服务连接的redis数据库编号
     * @param maijiToken 用户token
     * @return
     */
    public static AppUser getAppUserByRedis(RedisTemplate redisTemplate, Integer myDataBase, String maijiToken, UserInfoService userInfoService) {
        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        jedisConnectionFactory.setDatabase(0);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        AppUser appUser = (AppUser) redisTemplate.opsForValue().get("maijiToken:"+maijiToken);
        if (appUser == null) appUser = userInfoService.checkToken(maijiToken, false).getData();
        jedisConnectionFactory.setDatabase(myDataBase);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return appUser;
    }

    /**
     *   AppLogin微服务调用
     *  再redis数据库0中取出AppUser
     * @param redisTemplate 当前服务注入的redisTemplate
     * @param myDataBase 当前服务连接的redis数据库编号
     * @param maijiToken 用户token
     * @return
     */
    public static AppUser getAppUserByRedis(RedisTemplate redisTemplate, Integer myDataBase, String maijiToken, AppUserService appUserService) {
        JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        jedisConnectionFactory.setDatabase(0);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        AppUser appUser = (AppUser) redisTemplate.opsForValue().get("maijiToken:"+maijiToken);
        if (appUser == null) appUser = appUserService.checkToken(maijiToken);
        jedisConnectionFactory.setDatabase(myDataBase);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        return appUser;
    }

    /**
     *  获取redis数据库存放或获取数据的key值
     * @param redisNamespace 存储命名空间
     * @param keys 一个或多个key值
     * @return
     */
    public static String getRedisKey (String redisNamespace, String... keys) {
        String redisKey = redisNamespace + ":";
        for (String key: keys) {
            redisKey += key + "-";
        }
        return redisKey.substring(0, redisKey.length()-1);
    }

    /**
     *  将集合添加到Redis
     * @param stringRedisTemplate
     * @param redisKey
     * @param list
     */
    public static <T> void setList (StringRedisTemplate stringRedisTemplate, String redisKey, List<T> list) {
        stringRedisTemplate.delete(redisKey);
		for (T obj: list) {
            stringRedisTemplate.opsForList().rightPush(redisKey, JSON.toJSONString(obj));
            stringRedisTemplate.expire(redisKey, EXPIRELONG, TIMEUNIT);
		}
    }

    /**
     *  获取指定key集合指定区间的元素集合
     * @param stringRedisTemplate
     * @param redisKey
     * @param startIndex
     * @param endIndex
     * @param clazz
     * @return
     */
    public static <T> List<T> getList (StringRedisTemplate stringRedisTemplate, String redisKey, Integer startIndex, Integer endIndex, Class clazz) {
        List range = stringRedisTemplate.opsForList().range(redisKey, startIndex, endIndex);
        List<T> list = JSON.parseArray(range.toString(), clazz);
        return list;
    }

}
